/** \file
* Platform-independent implementations for the debug console class.
* This file is part of Chorus, available at
* http://code.google.com/p/chorus-twitter/ . This file and all associated files
* and derivative works are bound by the terms of the LGPL 3.0 license,
* available at http://www.gnu.org/licenses/lgpl-3.0-standalone.html .
* \par Subversion URL:
* \$URL: http://chorus-twitter.googlecode.com/svn/branches/cpp/src/tests/TestThreads.cpp $
* \version \$Rev: 152 $
* \date    \$Date: 2010-04-06 07:35:13 +0000 (Tue, 06 Apr 2010) $
*/

#include "config.h"

#include "system/Thread.hpp"
#include "system/Timer.hpp"
#include "system/DebugConsole.hpp"
#include "tests/TestThreads.hpp"

bool TestThreads::test()
{
	// Test a thread that returns immediately.
	{
		Thread thread(*this, &TestThreads::vfasttest);
		if (!thread.start())
			return false;
		if (!thread.waitThenAbort(50))
			return false;
	}
	// Test a thread that takes shorter (30ms) than the timeout value (50ms).
	{
		Thread thread(*this, &TestThreads::fasttest);
		if (!thread.start())
			return false;
		if (!thread.waitThenAbort(50))
			return false;
	}
	// Test a thread that takes longer (70ms) than the timeout value (50ms).
	{
		Thread thread(*this, &TestThreads::slowtest);
		if (!thread.start())
			return false;
		if (!thread.waitThenAbort(50))
			return false;
	}
	// Test a thread that freezes completely.
	{
		Thread thread(*this, &TestThreads::vslowtest);
		if (!thread.start())
			return false;
		if (!thread.waitThenAbort(50))
			return false;
	}

	return true;
}


bool TestThreads::vfasttest()
{
	PDEBUG("Very fast thread test");
	return true;
}

bool TestThreads::fasttest()
{
	PDEBUG("Fast thread test");
	Timer timer;
	while (timer.getTimeDouble() < 0.030);
	return true;
}

bool TestThreads::slowtest()
{
	PDEBUG("Slow thread test");
	Timer timer;
	while (timer.getTimeDouble() < 0.070);
	return true;
}

bool TestThreads::vslowtest()
{
	PDEBUG("Very slow thread test");
	for (;;);
}
